﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Janus.NorthwindDemo2007
{
    public partial class CategoryDataControl : Janus.NorthwindDemo2007.DataControl
    {
        
        protected override void OnDataSourceChanged()
        {

            this.AddBinding(this.editCategoryID, "Text", "Categories.CategoryID");
            this.AddBinding(this.editCategoryName, "Text", "Categories.CategoryName");
            this.AddBinding(this.editDescription, "Text", "Categories.Description");

            if (this.DataSource != null)
            {
                this.gridPicture.SetDataBinding(this.DataSource, "Categories");
                this.gridSmallIcon.SetDataBinding(this.DataSource, "Categories");
                this.gridLargeIcon.SetDataBinding(this.DataSource, "Categories");
            }

        }
        protected override void OnAllowEditChanged()
        {           
            this.editCategoryID.ReadOnly = !this.AllowEdit;
            this.editCategoryName.ReadOnly = !this.AllowEdit;
            this.editDescription.ReadOnly = !this.AllowEdit;
           
            this.btnSelectPicture.Enabled = this.AllowEdit;
            this.btnClearPicture.Enabled = this.AllowEdit;
            this.btnSelectSmallIcon.Enabled = this.AllowEdit;
            this.btnClearSmallIcon.Enabled = this.AllowEdit;
            this.btnSelectLargeIcon.Enabled = this.AllowEdit;
            this.btnClearLargeIcon.Enabled = this.AllowEdit;
        }
        
        private void RemoveImage(string fieldName)
        {
            if (gridPicture.CurrentRow != null)
            {
                gridPicture.SetValue(fieldName, DBNull.Value);
                gridPicture.UpdateData();
            }
        }
        private void AddImage(string sourceName, string fieldName, Janus.Windows.GridEX.GridEX grid)
        {
            if (this.IsAddNew)
            {
                try
                {
                    //If the current row is new, first try to update the current row.
                    CurrencyManager manager = (CurrencyManager)BindingContext[this.DataSource, sourceName];
                    manager.EndCurrentEdit();
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message, NorthwindApp.MessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
            if (this.openPhotoDialog.ShowDialog(this) == DialogResult.OK)
            {
                try
                {
                    Image img = Image.FromFile(openPhotoDialog.FileName);
                    if (img != null)
                    {
                        grid.SetValue(fieldName, img);
                        grid.UpdateData();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Invalid Image File", NorthwindApp.MessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
       
        private void btnSelectPicture_Click(object sender, EventArgs e)
        {
            AddImage("Categories", "Picture", gridPicture);
        }
        private void btnSelectSmallIcon_Click(object sender, EventArgs e)
        {
            AddImage("Categories", "SmallIcon", gridSmallIcon);
        }

        private void btnClearPicture_Click(object sender, EventArgs e)
        {
            RemoveImage("Picture");
        }

        private void btnClearSmallIcon_Click(object sender, EventArgs e)
        {
            RemoveImage("SmallIcon");
        }

        private void btnSelectLargeIcon_Click(object sender, EventArgs e)
        {
            AddImage("Categories", "LargeIcon", gridLargeIcon);
        }

        private void btnClearLargeIcon_Click(object sender, EventArgs e)
        {
            RemoveImage("LargeIcon");
        }

        


    }
}
